package com.ahcloud.edas.gateway.api.controller;

import com.ahcloud.edas.common.domain.common.PageResult;
import com.ahcloud.edas.common.domain.common.ResponseResult;
import com.ahcloud.edas.gateway.core.application.manager.GatewayPluginManager;
import com.ahcloud.edas.gateway.core.domain.plugin.form.PluginAddForm;
import com.ahcloud.edas.gateway.core.domain.plugin.form.PluginUpdateForm;
import com.ahcloud.edas.gateway.core.domain.plugin.query.PluginQuery;
import com.ahcloud.edas.gateway.core.domain.plugin.vo.PluginDetailVO;
import com.ahcloud.edas.gateway.core.domain.plugin.vo.PluginVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @program: edas
 * @description:
 * @author: YuKai Fan
 * @create: 2024/6/13 09:20
 **/
@RestController
@RequestMapping("/plugin")
public class PluginController {
    @Resource
    private GatewayPluginManager gatewayPluginManager;

    /**
     * 添加Plugin插件
     * @param form
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Void> addPlugin(@RequestBody @Valid PluginAddForm form) {
        gatewayPluginManager.createPlugin(form);
        return ResponseResult.ofSuccess();
    }

    /**
     * 更新Plugin插件
     * @param form
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<Void> updatePlugin(@RequestBody @Valid PluginUpdateForm form) {
        gatewayPluginManager.updatePlugin(form);
        return ResponseResult.ofSuccess();
    }

    /**
     * 删除Plugin插件
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    public ResponseResult<Void> deletePluginById(@PathVariable("id") Long id) {
        gatewayPluginManager.deletePlugin(id);
        return ResponseResult.ofSuccess();
    }

    /**
     * 根据id获取Plugin详情
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public ResponseResult<PluginDetailVO> findPluginById(@PathVariable("id") Long id) {
        return ResponseResult.ofSuccess(gatewayPluginManager.findPluginById(id));
    }

    /**
     * 分页查询Plugin列表
     * @param query
     * @return
     */
    @GetMapping("/page")
    public ResponseResult<PageResult<PluginVO>> pagePluginList(PluginQuery query) {
        return ResponseResult.ofSuccess(gatewayPluginManager.pagePluginList(query));
    }

    /**
     * 下线插件
     * @param id
     * @return
     */
    @PostMapping("/offlinePlugin/{id}/{env}")
    public ResponseResult<Void> offlinePlugin(@PathVariable("id") Long id, @PathVariable("env") String env) {
        gatewayPluginManager.offlinePlugin(id, env);
        return ResponseResult.ofSuccess();
    }

    /**
     * 上线插件
     * @param id
     * @return
     */
    @PostMapping("/onlinePlugin/{id}/{env}")
    public ResponseResult<Void> onlinePlugin(@PathVariable("id") Long id, @PathVariable("env") String env) {
        gatewayPluginManager.onlinePlugin(id, env);
        return ResponseResult.ofSuccess();
    }

    /**
     * 变更Plugin排序
     * @param id
     * @return
     */
    @PostMapping("/changePluginSort/{id}/{sort}")
    public ResponseResult<Void> changePluginSort(
            @PathVariable("id") Long id,
            @PathVariable("sort") Integer sort) {
        gatewayPluginManager.changePluginSort(id, sort);
        return ResponseResult.ofSuccess();
    }
}
